using System;
using CStock;
using System.Diagnostics;
using CUtil;

using System.ComponentModel;
using System.Collections.Generic;

namespace CStock
{
  
	/// <summary>
	/// Summary description for TradeSystemBase - serializable class
	/// </summary>
	/// 
	//[System.Xml.Serialization.XmlRootAttribute("TradeSystem", Namespace="", IsNullable=false)]
	[Serializable]
    public class Profile : StockObjectBase, IProfile
	{
        protected IList<ChartLine> chartLine;

        public override object Clone()
        {
            IProfile prof = new Profile();
            
            for (int i = 0; i < chartLine.Count; i++)
            {
                prof.ChartLine.Add(chartLine[i]);

            }
            return prof;
        }
		#region constructor, dispose

        public Profile()
		{
            ResetPathFromConfiguration();
            this.chartLine = new List<ChartLine>();
            this.iD = CUtil.TradeSystemFiles.Trade.ToString();
			this.fileName = CUtil.TradeSystemFiles.Trade.ToString() + ".xml";
			
           
			
		}
        public override bool Serialize()
        {
            ResetPathFromConfiguration();
            return base.Serialize();
        }
        public override bool ResetPathFromConfiguration()
        {

            ProgramContext pc = ProgramContext.Instance;
            Debug.Assert(pc != null);
            if (pc != null)
                this.path = pc.Configuration.Directories.ProfilePath;
            return true;

        }
		#endregion

		#region public properties

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public IList<ChartLine> ChartLine
        {
            get
            {

                return this.chartLine;
            }
            set
            {
                this.chartLine = value;

            }
        }
      
		
		//[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Serialization.XmlForm.Unqualified, IsNullable=false)]
		//[System.Xml.Serialization.XmlIgnoreAttribute()]
        public ChartLine[] ChartLines
		{
			get
			{
                List<ChartLine> myChartLine = chartLine as List<ChartLine> ;
                if (myChartLine != null && myChartLine.Count > 0)
                {
                    return (ChartLine[])myChartLine.ToArray();
                }
                return null;
			}
			set // provide empty setter - otherwise this won't be XML serialized
			{
                ChartLine[] myChartLines = value;
                chartLine = new List<ChartLine>();
                if (myChartLines != null)
                {
                    for (int i = 0; i < myChartLines.GetLength(0); i++)
                    {
                        chartLine.Add(myChartLines[i]);

                    }
                }


			}
	
		}
		
	
		#endregion

        public static bool CheckProfile(IDataItem di)
        {
            IList<string> strProfiles = GetProfiles();
            if(strProfiles.Contains(di.ProfileName))
            {
                return true;
            }
            else
            {
                IProfile prof = GetProfileDefault();
                if (prof != null)
                {
                    di.ProfileName = prof.ID;
                    di.Parent.Serialize();
                    return true;
                }
                return false;
            }
            
        }

        public static IProfile GetProfileByName(string myName)
        {

            string myDir = GetProfileDir();
            IProfile myProf = new Profile();
            myProf.ID = myName;
            //already set in constructor myProf.Path = myDir;
            object o = myProf;
            myProf.DeSerialize(ref o);

            return o as IProfile;
        }

        public static IList<string> GetProfiles()
        {

            string myDir = GetProfileDir();
            IList<string> myProfiles = new List<string>();
            string[] darr = System.IO.Directory.GetFiles(myDir);

            for (int i = 0; i < darr.GetLength(0); i++)
            {
                string myId = CUtil.IOUtil.ExtractNameWithoutExtensionFromFileName(darr[i]);
                
                myProfiles.Add(myId);
            }

           

            return myProfiles;
        }
        private static string GetProfileDir()
        {
            ProgramContext pc = ProgramContext.Instance;
            Debug.Assert(pc != null);
            string myDir = pc.Configuration.Directories.ProfilePath;
            Debug.Assert(!String.IsNullOrEmpty(myDir));
            if (!String.IsNullOrEmpty(myDir))
            {


                if (!System.IO.Directory.Exists(myDir))
                {
                    System.IO.Directory.CreateDirectory(myDir);
                }
                return myDir;
            }
            else
                return string.Empty;
        }
       
        public static IProfile GetProfileDefault()
        {

            string myDir = GetProfileDir();


            IList<string> myProfiles = new List<string>();
            string[] darr = System.IO.Directory.GetFiles(myDir);

            string defaultProfile = string.Empty;
            for (int i = 0; i < darr.GetLength(0); i++)
            {
                if (darr[i].Contains("Default"))
                {
                    defaultProfile = "Default";
                    break;
                }
                
            }

            if (String.IsNullOrEmpty(defaultProfile))
            {
                if (darr.GetLength(0) > 0)
                {
                   
                    
                    //string strDirectory = saveFileDialog1.FileName.Remove(saveFileDialog1.FileName.Length - strFileName.Length);
                    defaultProfile = CUtil.IOUtil.ExtractNameWithoutExtensionFromFileName(darr[0]);
                }
                else
                {
                    defaultProfile = "Default";
                    IProfile myProf = new Profile();
                    myProf.ID = defaultProfile;
                    myProf.Serialize();
                }


            }
            IProfile myProfNew = new Profile();
            myProfNew.ID = defaultProfile;
            //already set in constructor myProf.Path = myDir;
            object o = myProfNew;
            myProfNew.DeSerialize(ref o);

            return o as IProfile;
        }
    
      

	}
}
